package com.ssyt.blog.core.sys.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssyt.blog.core.common.PageModel;
import com.ssyt.blog.core.common.util.DateUtil;
import com.ssyt.blog.core.sys.dao.ArticleDao;
import com.ssyt.blog.core.sys.entity.Article;

@Service
public class ArticleService {
	@Autowired
	private ArticleDao articleDao;

	public PageModel<Article> list(int pageNo) {
		PageModel<Object[]> pageModel = articleDao.list(pageNo);
		List<Article> articleList = new ArrayList<>();
		List<Object[]> list = pageModel.getList();
		for (Object[] objects : list) {
			Article article = new Article();
			article.setId(Integer.parseInt(objects[0].toString()));
			article.setTitle(objects[1].toString());
			article.setContent(objects[2].toString());
			article.setCreateTime(DateUtil.parse(objects[3].toString(),
					"yyyy-MM-dd"));
			article.setReadTimes(Integer.parseInt(objects[4].toString()));
			article.setUserName(objects[5].toString());
			article.setPhoto(objects[6].toString());
			article.setUserId(Integer.parseInt(objects[7].toString()));
			article.setCategoryName(objects[8].toString());
			articleList.add(article);
		}
		PageModel<Article> articlePageModel = new PageModel<>();
		articlePageModel.setList(articleList);
		articlePageModel.setPageNo(pageModel.getPageNo());
		articlePageModel.setPageSize(pageModel.getPageSize());
		articlePageModel.setTotalRecords(pageModel.getTotalRecords());
		return articlePageModel;
	}

	public Article getArticle(int id) {
		return articleDao.getArticle(id);
	}

	public void addReadTimes(int id) {
		articleDao.addReadTimes(id);
	}

	public List<Article> allRankList() {
		return articleDao.allRankList();
	}

	public List<Article> authorRankList(int userId) {
		return articleDao.authorRankList(userId);
	}

	public PageModel<Article> getArticleList(int userId, int pageNo) {
		return articleDao.getArticleList(userId, pageNo);
	}

	public List<Object[]> categoryInfo(int userId) {
		return articleDao.categoryInfo(userId);
	}

	public PageModel<Article> getArticleByCategory(int categoryId, int userId,
			int pageNo) {
		return articleDao.getArticleByCategory(categoryId, userId, pageNo);
	}

	public void articleInfoDelete(Integer id) {
		articleDao.articleInfoDelete(id);

	}

	public Integer addArticle(Article article) {
		return articleDao.addArticle(article);
	}

}
